package com.foucus.myfouces.Base;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.foucus.myfouces.Resource.Util.ToastUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.ResponseBody;

/**
 * Created by yf-06 on 2017/6/29.
 */

public class Setting {
    private static String response;
    private static Setting setting;

    private static final int ERRORCODE = 0;
    private static final int SUCCESSCODE = 1;
    private static final int FAILCODE = 2;

    public Setting(Context mContext, String response) {
        this.response = response;
    }


    public static boolean isRight(Context mContext, String response) {
        setting = new Setting(mContext, response);
        switch (init()) {
            case SUCCESSCODE:
                return true;

            case FAILCODE:

                return false;
            case ERRORCODE:
                ToastUtil.show(mContext, "网络连接异常");
                return false;
            default:
                return false;
        }
    }

    public static int init() {
        try {
            JSONObject jsonObject = new JSONObject(response);
            String errno = jsonObject.optString("errno");
            String errmsg = jsonObject.optString("errmsg");
            Log.v("ssss", "errno==>" + errno);
            Log.v("ssss", "errmsg==>" + errmsg);

            if ("000".equals(errno)) {
                return SUCCESSCODE;
            } else if ("".equals(errno)) {
                return FAILCODE;
            } else {
                return FAILCODE;
            }
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("ssss", "连接出错==》" + e.toString());
            return ERRORCODE;
        }
    }
}
